#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<string>
#include<fcntl.h>

#define Die(code)   \
    do              \
    {               \
        exit(code); \
    } while (0)

#define CONV(v) (struct sockaddr *)(v)

enum
{
    USAGE_ERR = 1,
    SOCKET_ERR,
    BIND_ERR,
    LISTEN_ERR,
    CONNECT_ERR,
    EPOLL_CREATE_ERR,
    EPOLL_CTL_ERR
};

const int gdefaultsockfd = -1;
const int gbacklog = 8;

// 1.正常字符串  2.out非空串&&返回值是true 3.out是空串&&返回值是true //不考虑异常情况所以不考虑找不到sep的情况。
bool ParseOneLine(std::string &str, std::string *out, const std::string &sep)
{
    size_t pos = str.find(sep);
    if (pos == std::string::npos)
        return false;

    // 下面就是找到分隔符了
    *out = str.substr(0, pos);
    str.erase(0, pos + sep.size());
    return true;
}

bool SplitString(const std::string &header, const std::string &sep, std::string *key, std::string *value)
{
    auto pos = header.find(sep);
    if (pos == std::string::npos)
        return false;

    *key = header.substr(0, pos);
    *value = header.substr(pos + sep.size());
    return true;
}


//因为ET是非阻塞的，所以要设置一个公共接口
void SetNonBlock(int fd)
{
    int fl=fcntl(fd,F_GETFL);
    if(fl<0)
    {
        return;
    }
    fcntl(fd,F_SETFL,fl|O_NONBLOCK);
}